<?php

namespace System\Core;

if (!defined('BASEPATH')) die('Access Denied.');

/**
 * µMVC Framework (c) 2011.
 * 
 * PHP5 MVC Framework software for PHP 5.2.17 or later
 * 
 * @package         µMVC
 * @filename        system/core/view.class.php
 * @copyright       Lanz (c) 2011.
 * 
 */

class ViewException extends CommonException {};

/**
 * View class
 * The view class renders a specific page and holds local data used by the page.
 * 
 * @package         µMVC
 * @subpackage      Core
 * @category        System
 * 
 */
class View extends BaseSingleton
{

    function __construct()
    {
        Log::instance()->write(Log::DEBUG, 'View Class intitialized');
    }
    
    /**
     * Renders the specified page.
     * @param string $view page to render
     * @param array $data the data to include in the page.
     */
    public function render($view, $data = array())
    {
        // extract the $data array into local variables.
        extract($data);
        
        // Make sure that the view ends with php.
        $view = substr($view, -4) == '.php' ? $view : $view.'.php';
        $view = BASEPATH.'views'.DS.$view;

        if (is_readable($view))
        {
            include $view;
        }
        else
        {
            throw new ViewException("View page not found: $view");
        }
    }

    public function templateRender($template, $view, $title, $data = array())
    {
        $data['title'] = $title;
        // extract the $data array into local variables.
        extract($data);
                
        $template = substr($template, -4) == '.php' ? $template : $template.'.php';
        $template = BASEPATH.'templates'.DS.$template;
        
        if (is_readable($template))
        {
            include $template;
        }
        else
        {
            throw new ViewException("Template not found: $template");
        }
    }
}

?>
